<template>
    <avue-crud ref="crud"
               :page.sync="page"
               :data="tableData"
               :table-loading="tableLoading"
               :option="tableOption"
               @on-load="getList"
               @search-change="searchChange"
               @refresh-change="refreshChange"
               @selection-change="selectionChange">
        <!--<template slot="menuLeft">-->
            <!--<el-checkbox-group v-model="checkList">-->
                <!--<el-checkbox label="计算损耗" @change="countLoss($event)"></el-checkbox>-->
            <!--</el-checkbox-group>-->
        <!--</template>-->
        <template slot="menuRight">
            <el-button type="primary" icon="el-icon-edit" size="small" >打印出库单</el-button>
        </template>
        <template slot-scope="scope" slot="menu">
            <el-button type="text"
                       size="small"
                       plain
                       @click="jumpToDetail(scope.row)">详情
            </el-button>
            <el-button type="text"
                       size="small"
                       plain
                       @click="choiceExit(scope.row)">出库确认
            </el-button>
          </template>
          <template slot="menuLeft">
             <el-dialog title="请输入实际出库量" :visible.sync="dialogFormVisible">
                <el-form :model="form">
                    <el-form-item label="商品名称" :label-width="formLabelWidth">
                      <el-tag> {{form.goodsName}}</el-tag>
                    </el-form-item>
                    <el-form-item label="商品规格" :label-width="formLabelWidth">
                      <el-tag> {{form.modelName}}</el-tag>
                    </el-form-item>
                    <el-form-item label="单位" :label-width="formLabelWidth">
                      <el-tag> {{form.modelUnit}}</el-tag>
                    </el-form-item>
                    <el-form-item label="订单编号" :label-width="formLabelWidth">
                      <el-tag> {{form.goodsStorage}}</el-tag>
                    </el-form-item>
                    <el-form-item label="实际出库量" :label-width="formLabelWidth" v-for="item in form.detailList">
                        <el-input v-model="item.productNumber" autocomplete="off" style="width: 90%"></el-input>
                    </el-form-item>
                </el-form>
                <div slot="footer" class="dialog-footer">
                    <el-button @click="dialogFormVisible = false">取 消</el-button>
                    <el-button type="primary" @click="exitConfirm">确 定</el-button>
                </div>
            </el-dialog>
        </template>
    </avue-crud>
</template>

<script>
    import {
        fetchList,
        confirmRealExitNum
    }
        from '@/api/scm/order/exit_wait'
    import {
        tableOption
    } from '@/const/crud/scm/order/exit_wait'
    import {
        mapGetters
    } from 'vuex'

    import Util from '@/util/pds/utils'
    let cookieUtil = new Util();

    export default {
        name: "exitWait",
        data() {
            return {
                formLabelWidth: '',
                tableData: [],
                checkList: ['待采购商品'],
                page: {
                    total: 0, // 总页数
                    currentPage: 1, // 当前页数
                    pageSize: 20 // 每页显示多少条
                },
                tableLoading: false,
                orderData: [],
                //表格数据
                tableOption: tableOption,
                dialogFormVisible: false,
                form: {},
                pmId: this.getStore({name: "pmId"})
            }

        },
        computed: {
        },
        methods: {
            getList(page, params = {}) {
                params.pmId = this.pmId;
                // 默认配送日期
                // if(!params.deliveryTime){
                //     params.deliveryTime = cookieUtil.getDiffDate(1);
                // }
                this.tableLoading = true
                console.warn("sss", params);
                fetchList(params).then(response => {
                    if(response.data.code === 0){
                        this.tableData = this.transToInfo(response.data.data.list)
                        // this.page.total = response.data.data.total
                    }else{
                        this.tableData = [];
                    }
                    this.tableLoading = false
                })
            },

            /**
             * 数据转换
             **/
            transToInfo(list) {
                let result = [];
                list.forEach(item => {
                    let obj = {
                        goodsName: item.goodsModel.goodsName,
                        modelName: item.goodsModel.modelName,
                        modelUnit: item.goodsModel.modelUnit,
                        goodsStorage: item.goodsModel.modelStorage,
                        productNumber: item.productNumber,
                        deliveryDate: item.deliveryDate,
                        deliverySeq: item.deliverySeq,
                        goodsModelId: item.goodsModelId,
                        detailList: item.detailList,
                        orderNum: item.detailList.length
                    };
                    result.push(obj);
                });
                return result;
            },
            // //计算损耗
            // countLoss(e){
            //   if(e == true){
            //     this.tableData.forEach(item => {
            //       item.purchaseNum = item.purchaseNum* (1+item.lossRate);
            //     })
            //   }else{
            //     this.tableData.forEach(item => {
            //       item.purchaseNum = item.needBuyNum
            //     })
            //   }
            // },
            choiceExit(row){
                console.log(row);
                this.form= row;
                this.dialogFormVisible = true;
            },

            /**
             * 出库数量确认
             * */
            exitConfirm(){
                let detailList = [];
                this.form.detailList.forEach(item =>{
                  detailList.push({
                    pmId:item.pmId,
                    id:item.id,
                    productNumber:item.productNumber
                  })
                });
                let params = {
                    pmId : this.pmId,
                    goodsModelId : this.form.goodsModelId,
                    detailList : detailList
                };
                confirmRealExitNum(params).then(res =>{
                    if(res.data.code === 0){
                        this.dialogFormVisible = false;
                        this.$message({
                          showClose: true,
                          message: '出库成功',
                          type: 'success'
                        })
                        this.getList(this.page);
                    }
                });
            },

            /**
             * @title 数据更新
             * @param row 为当前的数据
             * @param index 为当前更新数据的行数
             * @param done 为表单关闭函数
             *
             **/
            handleUpdate: function (row, index, done) {
                putObj(row).then(data => {
                    this.tableData.splice(index, 1, Object.assign({}, row))
                    this.$message({
                        showClose: true,
                        message: '修改成功',
                        type: 'success'
                    })
                    done()
                    this.getList(this.page)
                })
            },
            /**
             * @title 数据添加
             * @param row 为当前的数据
             * @param done 为表单关闭函数
             *
             **/
            handleSave: function (row, done) {
                addObj(row).then(data => {
                    this.tableData.push(Object.assign({}, row))
                    this.$message({
                        showClose: true,
                        message: '添加成功',
                        type: 'success'
                    })
                    done()
                    this.getList(this.page)
                })
            },
            //移除
            handleClick(row) {
                console.log(row);
            },
            /**
             * 搜索回调
             */
            searchChange(form, done) {
                this.getList(this.page, form);
                done();
            },
            /**
             * 刷新回调
             */
            refreshChange(info) {
    this.getList(this.page, this.filterForm(info.searchForm));
},

            /**
             * 查看详情回调
             */
            jumpToDetail(row) {
                let query = {
                  row:row
                }
                this.$router.push({path: '/summary_order/stockupdetail',query:query})
            },
            selectionChange(list) {
                this.orderData = list
            }
        }
    }
</script>

<style scoped lang="scss">
</style>
